﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;

namespace RSSFeeds
{
    /*
    Developed By: Jyoti Seth
    Developed On: 24th Nov 2010
    Description: Generic functions to Get Current Web, Create new list, edit list, to get list,
                 to add new field to the list, to check if list or web exists.
    */
    public class SharepointManager
    {
        public static SPWeb GetCurrentWeb()
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = site.OpenWeb();

            //to enable editing from this website
            web.AllowUnsafeUpdates = true;

            return web;
        }

        public static Guid CreateNewList(string listName, string listDescription, SPListTemplateType type)
        {
            Guid guid = Guid.Empty;
            try
            {
                SPWeb web = GetCurrentWeb();
                guid = web.Lists.Add(listName, listDescription, type);
                web.Close();
            }
            catch { }
            return guid;
        }

        public static SPList GetList(string listName)
        {
            SPList list = null;
            try
            {
                SPWeb web = GetCurrentWeb();
                list = web.Lists[listName];
                web.Close();
            }
            catch { }
            return list;
        }

        public static bool ManageList(string listName, SPField field)
        {
            bool isSuccess = false;
            SPList list = GetList(listName);
            if (list.Fields.GetField(field.InternalName) == null)
                list.Fields.Add(field);

            return isSuccess;
        }

        public static SPListItem GetListItem(int itemId, string listName)
        {
            SPList list = GetList(listName);
            if (list != null)
                return list.Items.GetItemById(itemId);
            else
                return null;
        }

        public static void UpdateItem(SPListItem listItem, string listName)
        {
            SPList list = GetList(listName);
        }

        public static void AddCalendarEvent(string title, DateTime start, DateTime end, string description, string location)
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = site.OpenWeb();

            //to enable editing from this website
            web.AllowUnsafeUpdates = true;

            SPList calendar = web.Lists["Calendar"];
            SPListItem newEvent = null;

            newEvent = calendar.Items.Add();
            newEvent["Location"] = "Location A";
            newEvent["Start Time"] = DateTime.Now;
            newEvent["End Time"] = DateTime.Now.AddDays(2);
            newEvent["Description"] = "Task description goes here";
            newEvent["Title"] = title;

            SPUser user = web.SiteUsers.GetByID(web.CurrentUser.ID);
            if (user != null)
                newEvent["Assigned To"] = user.ID;

            newEvent.SystemUpdate();
            web.Close();
        }

        public static bool ListExists(SPWeb web, string listName)
        {
            return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title, listName));
        }

        public static bool WebExists(SPSite site, string webName)
        {
            return site.AllWebs.Cast<SPWeb>().Any(web => string.Equals(web.Name, webName));
        }

    }
}
